package com.edu.framework.websocket.handler;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSONArray;
import com.edu.framework.websocket.WebSocketHolder;
import com.edu.framework.websocket.message.base.Response;
import com.edu.framework.websocket.message.highlights.HighlightsRequest;
import com.edu.framework.websocket.message.info.InfoRequest;
import com.edu.framework.websocket.message.tclass.ClassRequest;
import com.edu.framework.websocket.message.tclass.ClassVO;
import com.edu.framework.websocket.message.terminal.TerminalRequest;
import com.edu.framework.websocket.message.terminal.TerminalVO;
import com.edu.framework.websocket.service.BaseInfoService;
import com.edu.system.domain.TClass;
import com.edu.system.service.ITClassService;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Component
public class ClassRequestHandler implements IHandler<ClassRequest> {

    @Resource
    private ITClassService classService;

    @Override
    public void execute(WebSocketSession session, ClassRequest message) {
        if (message == null) {
            return;
        }
        Map<String, String> data = message.getData();
        String id = data.get(ClassRequest.CLASS_ID_KEY);
        Response<ClassVO> response = null;

        if (id == null){
            response = Response.fail(HighlightsRequest.TYPE, "班级id不能为空");
        } else {
            TClass tClass = classService.selectTClassById(Long.parseLong(id));
            ClassVO classVO = BeanUtil.copyProperties(tClass, ClassVO.class);
            response = Response.ok(ClassRequest.TYPE, ClassRequest.RESP_KEY, classVO);
        }

        try {
            WebSocketHolder.send(session,response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public String getType() {
        return ClassRequest.TYPE;
    }
}
